/****************************************************************************
**  @copyright  TinKinG
**  @contract   lsnbing@126.com
**
**  @author TinKinG
**  @date   2020-3-12
**
**
*****************************************************************************/

#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDir>
#include <QDebug>
#include <QProcess>

#include "LineEdit.h"
#include "widget.h"
#include "ui_widget.h"
#include "PerspectiveView.h"
#include "ResizeWidget.h"

bool Widget::createDirByFullPath(const QString &fullPath)
{
    QFileInfo file(fullPath +"/p" );
    QDir filePath = file.path();
    if (filePath.exists()) {
        return true;
    }

    auto list = filePath.path().split("/");
    QString path;
    for (auto it = list.begin(); it != list.end(); ++it) {
        path += *it;
        path += "/";/* 现在win和非win都支持这个作为文件夹名称了，所有的文件夹名称方式均已*nix方式使用 */
        if(!filePath.exists(path))
            if(!filePath.mkdir(path)){
                return false;
            }
    }
    return true;
}

void Widget::save(const QImage &img,const QString &path,const QString &icnsName)
{
    if (icnsName.isEmpty()) {
        setWindowTitle("icnsName is empty");
        return;
    }

    QDir dir = path;
    QString fullPath = dir.path() + "/" + icnsName + ".iconset";
    qDebug() << createDirByFullPath(fullPath);

    img.scaled(16,16,Qt::IgnoreAspectRatio,Qt::SmoothTransformation).save(fullPath + "/icon_16x16.png");
    img.scaled(32,32,Qt::IgnoreAspectRatio,Qt::SmoothTransformation).save(fullPath + "/icon_16x16@2x.png");
    img.scaled(32,32,Qt::IgnoreAspectRatio,Qt::SmoothTransformation).save(fullPath + "/icon_32x32.png");
    img.scaled(64,64,Qt::IgnoreAspectRatio,Qt::SmoothTransformation).save(fullPath + "/icon_32x32@2x.png");
    img.scaled(128,128,Qt::IgnoreAspectRatio,Qt::SmoothTransformation).save(fullPath + "/icon_128x128.png");
    img.scaled(256,256,Qt::IgnoreAspectRatio,Qt::SmoothTransformation).save(fullPath + "/icon_128x128@2x.png");
    img.scaled(256,256,Qt::IgnoreAspectRatio,Qt::SmoothTransformation).save(fullPath + "/icon_256x256.png");
    img.scaled(512,512,Qt::IgnoreAspectRatio,Qt::SmoothTransformation).save(fullPath + "/icon_256x256@2x.png");
    img.scaled(512,512,Qt::IgnoreAspectRatio,Qt::SmoothTransformation).save(fullPath + "/icon_512x512.png");
    img.save(fullPath + "/icon_512x512@2x.png");

#ifdef Q_OS_MAC
    QProcess shell;
    shell.setReadChannel(QProcess::StandardOutput);
    shell.start("iconutil -c icns " + fullPath);
    shell.waitForReadyRead();
    QString result = shell.readAll();
    qDebug() << result;

    shell.close();

    shell.start("open " + fullPath);
    shell.waitForReadyRead();
    qDebug() << shell.readAll();
    setWindowTitle("merged ok!");
#endif
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    view = new PerspectiveView;

    mainLayout = new QVBoxLayout;

    mainLayout->addWidget(new QLabel("悬浮按钮查看使用方式"));
    initOneRow();

    initTwoRow();
    mainLayout->addWidget(new QLabel());

    this->setLayout(mainLayout);
    resize(800,600);

//        QPixmap img("/Users/sl/img.png");
//        img.scaled(600,420,Qt::KeepAspectRatio,Qt::SmoothTransformation).save("/Users/sl/test.png");
//    ResizeWidget *test = new ResizeWidget;
//    test->show();
}


void Widget::initOneRow()
{
    QHBoxLayout *firstLayout = new QHBoxLayout;

    logoEdit = new LineEdit;
    connect(logoEdit,&LineEdit::droped,this,&Widget::onMerged);

    firstLayout->addWidget(new QLabel("logo"));
    firstLayout->addWidget(logoEdit);

    nameEdit = new LineEdit;
    firstLayout->addWidget(new QLabel("Icns Name:"));
    firstLayout->addWidget(nameEdit);

    QPushButton *downBtn = new QPushButton;
    connect(downBtn,&QPushButton::clicked,this,&Widget::onSaveBtnClick);

    downBtn->setText("Save Merged Icns");
    downBtn->setToolTip(u8"将png文件拖到编辑框内，输入要生成的文件名，按下即可生成dmg的Icns图");
    firstLayout->addWidget(downBtn);

    mainLayout->addLayout(firstLayout);
}

void Widget::initTwoRow()
{
    //1024图片转icns文件
    QHBoxLayout *bottomLayout = new QHBoxLayout;

    pngEdit = new LineEdit;
    pngNewName = new LineEdit;

    bottomLayout->addWidget(new QLabel("origin png:"));
    bottomLayout->addWidget(pngEdit);
    bottomLayout->addWidget(new QLabel("icns Name:"));
    bottomLayout->addWidget(pngNewName);

    QPushButton *pngToIcns = new QPushButton;
    pngToIcns->setText("Png ➡️ Icns");
    pngToIcns->setToolTip(u8"将png文件拖到编辑框内，输入要生成的文件名，按下即可生成icns图");

    connect(pngToIcns,&QPushButton::clicked,this,&Widget::onPngToIcnsClick);
    bottomLayout->addWidget(pngToIcns);

    mainLayout->addLayout(bottomLayout);
}



Widget::~Widget()
{
    delete ui;
}

void Widget::onSaveBtnClick()
{
    QFileInfo originInfo(logoEdit->text());
    QString path = originInfo.path();
    QString icnsName = nameEdit->text();
    save(view->getImage(),path,icnsName);
}

void Widget::onPngToIcnsClick()
{
    QImage img(pngEdit->text());

    QFileInfo info = pngEdit->text();

    if (img.size().width() != img.size().height() && img.size().width() != 1024) {
        setWindowTitle("image size not equal 1024!");
        return;
    }

    save(img,info.path(),pngNewName->text());
}

void Widget::onMerged()
{
    view->setMainImg(QImage("://pan_512x512@2x.png"));
    view->setTopImg(QImage(logoEdit->text()));
    view->show();
}




